package com.thunder.rebot.rebotclient.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.thunder.rebot.rebotclient.R;
import com.thunder.rebot.rebotclient.entity.InfoData;
import com.thunder.rebot.rebotclient.utils.LogUtil;
import com.thunder.rebot.rebotclient.utils.TRCommonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yuechunan on 18-7-2.
 */

public class DisplayListAdapter extends RecyclerView.Adapter<DisplayListAdapter.ViewHolder> {
    private static final String TAG = "DisplayListAdapter";
    private final Context mContext;
    private List<Map<String, Object>> selectDatas;
    private List<InfoData> datas;
    private ViewHolder holder;
    private CheckItemListener mCheckListener;

    public DisplayListAdapter(Context context, List<InfoData> data,List<Map<String, Object>> selectData,CheckItemListener mCheckListener) {
        this.mContext = context;
        this.datas = data;
        this.selectDatas=selectData;
        this.mCheckListener = mCheckListener;
        for(int i=0;i<datas.size();i++){
            Map<String,Object> map = new HashMap<String,Object>();
//                map.put("bit", bm);
            map.put("path", datas.get(i).getId());
            map.put("check", false);
            selectDatas.add(map);
        }
    }

    public void notificationList(List<InfoData> data,List<Map<String, Object>> selectData) {
        this.datas = data;
        this.selectDatas=selectData;
        LogUtil.i(TAG, "notificationList");
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LogUtil.i(TAG, "onCreateViewHolder");
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list, parent, false);
        holder = new ViewHolder(rootView);
        holder.rootView = rootView;
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
//        LogUtil.i(TAG, "onBindViewHolder"+ Arrays.toString(datas.get(position).getPicByte()));
        //图片加载框架
//        Glide.with(mContext).load((String)datas.get(position).getPicPath()).into(viewHolder.img);
        Bitmap bitmap = TRCommonUtil.byteToBitmap(datas.get(position).getPicByte());
        viewHolder.img.setImageBitmap(bitmap);
        viewHolder.check.setChecked((Boolean) selectDatas.get(position).get("check"));
        viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (null != mCheckListener) {
                    mCheckListener.itemChecked(position, b);
                }
            }
        });
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return null == datas ? 0 : datas.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView img;
        public CheckBox check;
        public View rootView;

        public ViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img_item);
            check = (CheckBox) itemView.findViewById(R.id.checkbox);
        }
    }

    public interface CheckItemListener {
        void itemChecked(int position, boolean isChecked);
    }

}
